package cn.stylefeng.guns.modular.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjArtistAuthenticatioInfo;
import cn.stylefeng.guns.modular.entity.IdjArtistAuthentication;
import cn.stylefeng.guns.modular.entity.IdjUserInfo;
import cn.stylefeng.guns.modular.entity.IdjUserRole;
import cn.stylefeng.guns.modular.entity.responseVo.ActivityApplyVo;
import cn.stylefeng.guns.modular.entity.responseVo.InfoVo;
import cn.stylefeng.guns.modular.entity.responseVo.TypeVo;
import cn.stylefeng.guns.modular.mapper.IdjArtistAuthenticatioInfoMapper;
import cn.stylefeng.guns.modular.mapper.IdjArtistAuthenticationMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserInfoMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserRoleMapper;
import cn.stylefeng.guns.modular.service.IIdjArtistAuthenticationService;
import cn.stylefeng.guns.modular.util.json.ArrayUtil;
import cn.stylefeng.guns.modular.util.json.JsonUtils;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * <p>
 * 艺人认证表 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-30
 */
@Service
public class IdjArtistAuthenticationServiceImpl extends ServiceImpl<IdjArtistAuthenticationMapper, IdjArtistAuthentication> implements IIdjArtistAuthenticationService {

	@Autowired
	private IdjArtistAuthenticationMapper mapper;
	
	@Autowired
	private IdjArtistAuthenticatioInfoMapper InfoMapper;
	
	@Autowired
	private IdjUserRoleMapper roleMapper;
	
	@Autowired
	private IdjUserInfoMapper userInfoMapper;
	
	private static  final int SHORT_MAX=65536;
	 
	private static int counter=-1;
	
	@Override
	public LayuiPageInfo getList(Page<IdjArtistAuthentication> page, String keyword, String beginTime, String endTime,
			String yes, String no, Integer isAudit) {
		Page<Map<String,Object>> list = mapper.getList(page, keyword, beginTime, endTime, yes, no, isAudit);
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long realAuthenticationId) {
		IdjArtistAuthentication authentication = mapper.selectByKeyId(realAuthenticationId);
		if(authentication != null){
			if(ToolUtil.isNotEmpty(authentication.getIsAudit())){
				if(authentication.getIsAudit() == 1){
					authentication.setReasonTypeName("已通过");
				}else{
					authentication.setReasonTypeName("已拒绝");;	
				}
			}
			if(ToolUtil.isNotEmpty(authentication.getServiceType())){
				if(authentication.getServiceType().equals("1")){
					authentication.setServiceType("嘉宾演出");
				}else{
					authentication.setServiceType("驻场演出");
				}
			}
			if(ToolUtil.isNotEmpty(authentication.getCity())){
				authentication.setCityName(authentication.getCity());
			}	
			if(ToolUtil.isNotEmpty(authentication.getRegion())){
				if(ToolUtil.isNotEmpty(authentication.getRegion()) && ToolUtil.isNotEmpty(authentication.getCityName())){
					authentication.setCityName(authentication.getCityName()+'-'+authentication.getRegion());
				}else{
					authentication.setCityName(authentication.getRegion());
				}
			}	
			if(ToolUtil.isNotEmpty(authentication.getImportantEvent())){
				authentication.setVo(JsonUtils.toObject(authentication.getImportantEvent(), InfoVo.class));
			}
			if(authentication.getGender() == 1){
				authentication.setGenderName("男");
			}else if(authentication.getGender() == 2){
				authentication.setGenderName("女");
			}else{
				authentication.setGenderName("未知");
			}
		}
		List<IdjArtistAuthenticatioInfo> list = InfoMapper.selectByKeyId(realAuthenticationId);
		List<IdjArtistAuthenticatioInfo> lista = new ArrayList<IdjArtistAuthenticatioInfo>();
		List<IdjArtistAuthenticatioInfo> listb = new ArrayList<IdjArtistAuthenticatioInfo>();
		List<IdjArtistAuthenticatioInfo> listc = new ArrayList<IdjArtistAuthenticatioInfo>();
		List<IdjArtistAuthenticatioInfo> listd = new ArrayList<IdjArtistAuthenticatioInfo>();
		if(list.size() > 0){
			for (int i = 0; i < list.size(); i++) {
				if(list.get(i).getInfoType().equals("证书") || list.get(i).getInfoType().equals("个性照片")){
					lista.add(list.get(i));
				}
				if(list.get(i).getInfoType().equals("混音set")){
					listb.add(list.get(i));
				}
				if(list.get(i).getInfoType().equals("原创作品")){
					listc.add(list.get(i));
				}
				if(list.get(i).getInfoType().equals("影音")){
					listd.add(list.get(i));
				}
			}
		}
		List<TypeVo> typeList = new ArrayList<TypeVo>();
		typeList.add(getType(lista, "证书和奖项照片"));
		typeList.add(getType(listb, "证混音set"));
		typeList.add(getType(listc, "原创作品"));
		typeList.add(getType(listd, "影音"));
		authentication.setVos(typeList);
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("authentication", authentication);
        return modelAndView;
	}

	@Override
	public int setIsAudit(IdjArtistAuthentication cation) {
		int i = mapper.setIsAudit(cation);
		if(cation.getIsAudit() == 1){
			IdjUserRole role = new IdjUserRole();
			role.setUserId(cation.getUserId());
			role.setUserRole(1);
			role.setCreateTime(new Date());
			roleMapper.insert(role);
			List<IdjArtistAuthenticatioInfo> list = InfoMapper.selectByKeyId(cation.getArtistAuthenticatioId());
			if(list.size() > 0){
				for (int j = 0; j < list.size(); j++) {
					IdjUserInfo info = new IdjUserInfo();
					info.setUserInfoId(nextId());
					info.setUserId(cation.getUserId());
					info.setInfoType(list.get(j).getInfoType());
					info.setInfoUrl(list.get(j).getInfoUrl());
					info.setCreateTime(new Date());
					info.setInfoName(list.get(j).getInfoName());
					userInfoMapper.insert(info);
				}
			}
		}
		return i;
	}
	
	public static synchronized long nextId() {
        long now = System.currentTimeMillis();
        if (counter == -1) {
            long seed = now ^ Thread.currentThread().getId();
            Random rnd = new Random(Long.hashCode(seed));
            counter = rnd.nextInt(SHORT_MAX);
        }
        long id = (now << 16) | counter;
        counter = (counter + 1) % SHORT_MAX;
        return id;
    }
	
	private  TypeVo getType (List<IdjArtistAuthenticatioInfo> lists, String lx){
		TypeVo vo = new TypeVo();
		vo.setType(lx);
		vo.setCount(lists.size());
		vo.setInfo(lists);
		return vo;
	}
	
}
